SlideShow = function( slideShowObjectsParam, navObjectsParam, transitionDurationParam, fadeDurationParam, mouseFadeDurationParam, mouseOverDelayParam, navOffClassParam, navOnClassParam, playAfterInit, parallel, maxFadeInParam)
{
	this.slideShowObjects = slideShowObjectsParam;
	this.navObjects = navObjectsParam;
	this.currentSlide = 1;
	this.defaultDiv = this.slideShowObjects[this.currentSlide - 1];
	this.transitionDuration = 5000;
	this.parallel = false;
	if(transitionDurationParam != null )
	{
		this.transitionDuration = transitionDurationParam;
	}
	if(parallel) {
		this.parallel = parallel;
	}
	
	this.fadeDuration = 0.8;
	if(fadeDurationParam != null)
	{
		this.fadeDuration = fadeDurationParam;
	}
	this.mouseFadeDuration = 0.5;
	if(mouseFadeDurationParam != null)
	{
		this.mouseFadeDuration = mouseFadeDurationParam;
	}
	this.mouseOverDelay = 200;
	if(mouseOverDelayParam != null)
	{
		this.mouseOverDelay = mouseOverDelayParam;
	}
	this.navOffClass = "";
	if(navOffClassParam != null)
	{
		this.navOffClass = navOffClassParam;
	}
	this.navOnClass = "";
	if(navOnClassParam != null)
	{
		this.navOnClass = navOnClassParam;
	}
	
	this.maxFadeIn = 1;
	if(maxFadeInParam != null)
	{
		this.maxFadeIn = maxFadeInParam;
	}
	
	this.slideShowTimeout = null;
	this.mouseOverTimeout = null;
	this.effectInTransition = false;
	
	this.init();
	
	if(playAfterInit)
	{
		var thisSlideShow = this;
		this.mouseOverTimeout = setTimeout(function(){thisSlideShow.play()}, this.transitionDuration)
	}
}

//Methods
SlideShow.prototype = {
		
	init: function()
	{
		/*for(var i=0; i < this.slideShowObjects.length; i++)
		{
			if(i != (this.currentSlide - 1)) {
				var slide = this.slideShowObjects[i];
				
			}
		}*/
		if(this.navObjects) {
			var mouseOver = this.mouseOverHandler.bind(this);
			var mouseOut = this.mouseOutHandler.bind(this);
			for(var i=0; i < this.navObjects.length; i++)
			{
				var navObject = this.navObjects[i];
				navObject.observe("mouseover", mouseOver.curry(i));
				navObject.observe("mouseout", mouseOut);
			}
		}
	},

	
	play: function()
	{
		
		var fadeOutDiv = null;
		var currentNav = null;
		if( this.getCurrentSlideDiv() != null )
		{
			//fade out
			fadeOutDiv = this.getCurrentSlideDiv();
			currentNav = this.getCurrentNavObject();
		}
		
		var fadeInDiv = this.getNextSlideDiv();
		var nextNav = this.getNextNavObject();
		
		this.fadeInOut(fadeOutDiv, fadeInDiv, currentNav, nextNav, this.fadeDuration);
		
		this.incrementCurrentSlide();

		var thisSlideShow = this;
		this.slideShowTimeout = setTimeout(function(){thisSlideShow.play()}, this.transitionDuration)
	},
	
	
	fadeInOut: function( fadeOutDiv, fadeInDiv, currentNav, nextNav, duration )
	{
		var thisSlideShow = this;
		
		if( fadeOutDiv != null )
		{
			this.effectInTransition = true;
			if(!thisSlideShow.parallel) {
				fadeOutDiv.show();
				fadeOutDiv.fade({ 
							  duration: duration, 
							  from: thisSlideShow.maxFadeIn, 
							  to: 0.1,
							  fps: 75,
							  afterFinish: function()
				  			  {
									if(currentNav) {
										currentNav.className = thisSlideShow.navOffClass;
										nextNav.className = thisSlideShow.navOnClass;
									}
									
									fadeOutDiv.hide();
									fadeInDiv.removeClassName("hidden-slide");
									fadeInDiv.setOpacity(.1);
									fadeInDiv.show();
									fadeInDiv.fade({ 
													  duration: duration, 
													  from: 0.1, 
													  to: thisSlideShow.maxFadeIn,
													  fps: 75,
													  afterFinish: function()
													  {
														thisSlideShow.effectInTransition = false;
													  }
													});
								}
						});
			} else {
				fadeOutDiv.show();
				fadeInDiv.hide();
				fadeInDiv.setOpacity(.1);
				fadeInDiv.removeClassName("hidden-slide");
				fadeInDiv.show();
				new Effect.Parallel(
						[
						 	new Effect.Fade(fadeOutDiv,
						 			{
										  from: thisSlideShow.maxFadeIn, 
										  to: 0.01
						 			}
						 		),
						 	new Effect.Appear(fadeInDiv, 
						 			{
										  from: 0.01, 
										  to: thisSlideShow.maxFadeIn
						 			}
						 	)
						], {
			 				  duration: duration, 
							  fps: 100,
							  afterFinish: function()
							  {
								fadeOutDiv.hide();
								thisSlideShow.effectInTransition = false;
							  }
						}
				)
			}
		}
		else
		{
			this.effectInTransition = true;
			if(nextNav) {
				nextNav.className = this.navOnClass;
			}
			fadeInDiv.hide();
			fadeInDiv.removeClassName("hidden-slide");
			fadeInDiv.setOpacity(.1);
			fadeInDiv.show();
			fadeInDiv.fade({ 
							  duration: duration, 
							  from: 0.1, 
							  to: 0.99,
							  afterFinish: function()
							  {
								thisSlideShow.effectInTransition = false;
							  }
							});
		}
	},
	
	
	mouseOverHandler: function( index )
	{
		var thisSlideShow = this;
		if(this.mouseOverTimeout != undefined && this.mouseOverTimeout != null)
		{
			clearTimeout(this.mouseOverTimeout);
		}
		this.mouseOverTimeout = setTimeout(function()
										   {
												if(!thisSlideShow.effectInTransition)
												{
													if(thisSlideShow.slideShowTimeout != undefined && thisSlideShow.slideShowTimeout != null)
													{
														thisSlideShow.effectInTransition = false;
														clearTimeout(thisSlideShow.slideShowTimeout);
													}
													
													var fadeOutDiv = thisSlideShow.getCurrentSlideDiv();
													var currentNav = thisSlideShow.getCurrentNavObject();
													
													var fadeInDiv = thisSlideShow.slideShowObjects[index];
													if(thisSlideShow.navObjects) {
														var nextNav = thisSlideShow.navObjects[index];
													}
													
													thisSlideShow.currentSlide = index + 1;
													
													if(fadeOutDiv != fadeInDiv)
													{
														currentNav.className = thisSlideShow.navOffClass;
														nextNav.className = thisSlideShow.navOnClass;
														
														thisSlideShow.fadeInOut(fadeOutDiv, fadeInDiv, currentNav, nextNav, thisSlideShow.mouseFadeDuration);
													}
												}
										   }, this.mouseOverDelay)
	},
	
	
	mouseOutHandler: function()
	{
		if(this.mouseOverTimeout != undefined && this.mouseOverTimeout != null)
		{
			clearTimeout(this.mouseOverTimeout);
		}
	},
	
	
	/* ***************************************** */
	/* ************ HELPER METHODS ************ */
	/* *************************************** */
	incrementCurrentSlide: function()
	{
		if( this.currentSlide + 1 > this.slideShowObjects.length )
		{
			this.currentSlide = 1;
		}
		else
		{
			this.currentSlide = this.currentSlide + 1;
		}
	},
	
	
	getCurrentSlideDiv: function()
	{
		var currentSlideDiv;
		
		if( this.currentSlide == 0)
		{
			currentSlideDiv = null;
		}
		else
		{
			currentSlideDiv = this.slideShowObjects[this.currentSlide - 1];
		}
		
		return currentSlideDiv;
	},
	
	
	getCurrentNavObject: function()
	{
		var currentNavObject;
		
		if( this.currentSlide == 0 || !this.navObjects)
		{
			currentNavObject = null;
		}
		else
		{
			currentNavObject = this.navObjects[this.currentSlide - 1];
		}
		
		return currentNavObject;
	},
	
	
	getNextSlideDiv: function()
	{
		var nextSlideDiv;
		
		if( this.currentSlide == 0 )
		{
			nextSlideDiv = this.slideShowObjects[0];
		}
		else
		{
			if( this.currentSlide + 1 > this.slideShowObjects.length )
			{
				nextSlideDiv = this.slideShowObjects[0];
			}
			else
			{
				nextSlideDiv = this.slideShowObjects[this.currentSlide];
			}
		}
		
		return nextSlideDiv;
	},
	
	
	getNextNavObject: function()
	{
		var nextNavObject;
		if(!this.navObjects) {
			return null;
		}
		
		if( this.currentSlide == 0 )
		{
			nextNavObject = this.navObjects[0];
		}
		else
		{
			if( this.currentSlide + 1 > this.navObjects.length )
			{
				nextNavObject = this.navObjects[0];
			}
			else
			{
				nextNavObject = this.navObjects[this.currentSlide];
			}
		}
		
		return nextNavObject;
	}
}
